﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;


namespace Excel2Word
{
    public partial class FrmDefineWordReport : Form
    {
        public const string rp_guid = "guid";
        public const string rp_report_template_name = "report_template_name";
        public const string rp_update_time = "update_time";
        public const string rp_server_file_name = "server_file_name";
        public const string rp_belong_model_id = "belong_model_id";


        private dynamic acDoc = null;
        private string modelId;
        private DataRow drReportModel; //报告模版信息

        private const string C_ZU = "组";
        private const string C_PHOTO = "拍照";


        public FrmDefineWordReport(Word.Document acDoc)
        {
            this.acDoc = acDoc;
            InitializeComponent();

            AppStore.deSerilize();

        }


        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmDefineReport_Load(object sender, EventArgs e)
        {

            //初始化树
            tvForFields.BeginUpdate();

            foreach (SourceElement se in AppStore.GetTestData())
            {
                var newNode = new TreeNode();
                newNode.Name = se.fieldName;
                newNode.Text = se.fieldName;
                newNode.Tag = se;
                tvForFields.Nodes.Add(newNode);
            }
            
            tvForFields.ExpandAll();
            tvForFields.EndUpdate();

        }

        /// <summary>
        /// 快速选取已打开的激活文档
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btFileSelect_Click(object sender, EventArgs e)
        {
            //获取当前激活文档
            var wordApp = WordOperation.GetActiveWord();
            if (wordApp != null && wordApp.ActiveDocument != null)
            {
                acDoc = wordApp.ActiveDocument;
                this.tbFilePath.Text = acDoc.Name;
            }
            else
            {
                MessageBox.Show("当前没有打开的WORD文档！");
            }
        }


        /// <summary>
        /// 双击某个节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {

        }


        private void btGetAcDoc_Click(object sender, EventArgs e)
        {
            //获取当前激活文档
            var wordApp = WordOperation.GetActiveWord();
            if (wordApp != null && wordApp.ActiveDocument != null)
            {
                acDoc = wordApp.ActiveDocument;
                this.tbFilePath.Text = acDoc.Name;
            }
            else
            {
                MessageBox.Show("当前没有打开的WORD文档！");
            }
        }

        private void pbFindDoc_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                var fullName = this.openFileDialog1.FileName;

                if (System.IO.File.Exists(fullName))
                {
                    acDoc = WordOperation.GetDocByFullName(fullName);
                    this.tbFilePath.Text = acDoc.Name;
                    acDoc.Activate();
                    acDoc.Application.Visible = true;
                    acDoc.Application.Activate();
                }
            }
        }


        /// <summary>
        /// 提交模版
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btUpload_Click(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// 点击某个节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvFields_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                if (e.Node.Nodes == null || e.Node.Nodes.Count == 0)
                {
                    if (acDoc != null)
                    {
                        
                        acDoc.Application.Activate();
                        //如果不是图片，则进行域操作 by wanglong
                        SourceElement se = e.Node.Tag as SourceElement;
                        if (se.filedType.Equals(SourceElement.FIELD_TYPE_NORMAL))
                        {
                            //设定域的类型为DocVariable
                            object objType = 64;// Microsoft.Office.Interop.Word.WdFieldType.wdFieldDocVariable;
                            //设定域的名称
                            object objText = Dics.WORD_FIELD_TAG + e.Node.Name;
                            object objMissing = false;

                            acDoc.Variables[objText].Value = "{" + e.Node.Text + "}";

                            //添加域
                            var filed = acDoc.Fields.Add(acDoc.Application.Selection.Range,
                                ref objType, ref objText, ref objMissing);

                            //设定域的值

                            acDoc.Application.Selection.MoveEnd();

                            //更新
                            acDoc.Fields.Update();


                        }
                        else if (se.filedType.Equals(SourceElement.FIELD_TYPE_PIC)) //如果是图片，则插入图片, 并用title来关联图片为后续替换做准备 by wanglong
                        {
                            string trueName = "";
                            if (e.Node.Parent != null)
                            {
                                trueName += e.Node.Parent.Text + "_";
                            }
                            trueName += e.Node.Text;

                            var shape = acDoc.InlineShapes.AddPicture(System.Windows.Forms.Application.StartupPath + "\\" + "nhimg.jpg", Type.Missing, Type.Missing, acDoc.Application.Selection.Range);

                            shape.Title = Dics.WORD_FIELD_TAG + e.Node.Tag.ToString() + trueName;
                            acDoc.Application.Selection.SetRange(acDoc.Application.Selection.Range.Start + 1, acDoc.Application.Selection.Range.End + 1);

                        }
                        else //如果是表格
                        {

                        }

                    }
                }
                else
                {
                    MessageBox.Show("没有激活的WORD文档");
                }
            }
        }

    }
}
